Midterm - Interactive Map¶

In [33]:
import pandas as pd
import geopandas as gpd
from shapely import wkt
import branca.colormap as cm
import folium

Coerce to GDF, extract longitude and latitude from geometry column

In [29]:
df = pd.read_excel("output_JF.xlsx")

df['geometry'] = df['geometry'].apply(wkt.loads)
gdf = gpd.GeoDataFrame(df, geometry='geometry')
gdf.set_crs(epsg=4326, inplace=True)

gdf_projected = gdf.to_crs(epsg=2163)
gdf_projected['centroid'] = gdf_projected.geometry.centroid
centroids_geo = gdf_projected['centroid'].to_crs(epsg=4326)

gdf['longitude'] = centroids_geo.x
gdf['latitude'] = centroids_geo.y
In [38]:
gdf.head()
gdf.columns
gdf.snap_rate
Out[38]:
0       2.3546033
1       8.3728724
2       2.6860058
3       7.4349442
4       2.5510204
          ...    
1582    15.929909
1583    3.2005122
1584    1.6909029
1585    2.7078257
1586    4.1017227
Name: snap_rate, Length: 1587, dtype: object

Get proper data types

In [46]:
gdf['snap_rate'] = gdf['snap_rate'].astype(float)
gdf['chd_pct'] = gdf['chd_pct'].astype(float)

Create map with snap_rate as the fill variable

In [43]:
snap_rate_min = gdf['snap_rate'].min()
snap_rate_max = gdf['snap_rate'].max()

colormap = cm.linear.YlOrRd_09.scale(snap_rate_min, snap_rate_max)
colormap.caption = 'SNAP Rate'

def style_function(feature):
    snap_rate = feature['properties']['snap_rate']
    return {
        'fillColor': colormap(snap_rate),
        'color': 'black',
        'weight': 1,
        'fillOpacity': 0.7
    }

m = folium.Map(location=[gdf['latitude'].mean(), gdf['longitude'].mean()], zoom_start=5)

folium.GeoJson(
    gdf,
    style_function=style_function,
    tooltip=folium.GeoJsonTooltip(fields=['snap_rate', 'chd_pct'], 
                                  aliases=['SNAP Rate:', 'CHD %:'], 
                                  localize=True),
    popup=folium.GeoJsonPopup(fields=['snap_rate', 'chd_pct'], 
                              aliases=['SNAP Rate:', 'CHD %:'], 
                              labels=True)
).add_to(m)

colormap.add_to(m)

m
Out[43]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Create map with chd_pct as the fill variable

In [48]:
chd_pct_min = gdf['chd_pct'].min()
chd_pct_max = gdf['chd_pct'].max()

colormap = cm.linear.YlOrRd_09.scale(chd_pct_min, chd_pct_max)
colormap.caption = 'CHD Rate'

def style_function(feature):
    chd_pct = feature['properties']['chd_pct']
    return {
        'fillColor': colormap(chd_pct),
        'color': 'black',
        'weight': 1,
        'fillOpacity': 0.7
    }

m = folium.Map(location=[gdf['latitude'].mean(), gdf['longitude'].mean()], zoom_start=5)

folium.GeoJson(
    gdf,
    style_function=style_function,
    tooltip=folium.GeoJsonTooltip(fields=['snap_rate', 'chd_pct'], 
                                  aliases=['SNAP Rate:', 'CHD %:'], 
                                  localize=True),
    popup=folium.GeoJsonPopup(fields=['snap_rate', 'chd_pct'], 
                              aliases=['SNAP Rate:', 'CHD %:'], 
                              labels=True)
).add_to(m)

colormap.add_to(m)

m
Out[48]:
Make this Notebook Trusted to load map: File -> Trust Notebook